home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / SCRIPT.C < prev    next >
C/C++ Source or Header  |  1991-07-06  |  11KB  |  323 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s c r i p t . c                                                 */
  3. /*                                                                    */
  4. /*    Script processing routines for UUPC/extended                    */
  5. /*                                                                    */
  6. /*    John H. DuBois III  3/31/90                                     */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. /*--------------------------------------------------------------------*/
  10. /*                        System include files                        */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17.  
  18. /*--------------------------------------------------------------------*/
  19. /*                    UUPC/extended include files                     */
  20. /*--------------------------------------------------------------------*/
  21.  
  22. #include "lib.h"
  23. #include "dcp.h"
  24. #include "dcpsys.h"
  25. #include "hostable.h"
  26. #include "modem.h"
  27. #include "script.h"
  28. #include "security.h"
  29. #include "ssleep.h"
  30. #include "ulib.h"
  31.  
  32. /*--------------------------------------------------------------------*/
  33. /*                           Local defines                            */
  34. /*--------------------------------------------------------------------*/
  35.  
  36. #define prefix(small,large)   (equaln((small), (large), strlen(small)))
  37. #define notin(str,log)        (strstr((log), (str)) == nil(char))
  38. #define MAXMATCH 64              /* max length of search string; must
  39.                                     be a power of 2                  */
  40. #define QINDMASK (MAXMATCH - 1)  /* bit mask to get queue index      */
  41.  
  42. #define EOTMSG "\004\r\004\r"
  43.  
  44. /*--------------------------------------------------------------------*/
  45. /*                    Internal function prototypes                    */
  46. /*--------------------------------------------------------------------*/
  47.  
  48. static int StrMatch(char *MatchStr,char C);
  49.                                  /* Internal match routine           */
  50. static boolean writestr(register char *s);
  51.  
  52. /*
  53.  *       e x p e c t s t r
  54.  *
  55.  *       wait for a pattern on input
  56.  *
  57.  *
  58.  *       expectstr reads characters from input using sread, and
  59.  *       compares them to a search string.  It reads characters until
  60.  *       either the search string has been seen on the input or a
  61.  *       specified timeout interval has passed without any characters
  62.  *       being read.
  63.  *
  64.  *      Global variables: none.
  65.  *
  66.  *      Input parameters:
  67.  *      Search is the string that is searched for on the input.
  68.  *      Timeout is the timeout interval passed to sread.
  69.  *
  70.  *      Output parameters: none.
  71.  *
  72.  *      Return value:
  73.  *      TRUE is returned if the search string is found on input.
  74.  *      FALSE is returned if sread times out.
  75.  */
  76.  
  77. boolean expectstr(char *Search, unsigned int Timeout)
  78. {
  79.         char buf[BUFSIZ];
  80.         register char *ptr = buf;
  81.  
  82.         printmsg(1, "wanted \"%s\"", Search);
  83.  
  84.         if (!strlen(Search))                      /* expects nothing */
  85.             return TRUE;
  86.  
  87.         StrMatch(Search,'\0');   /* set up search string in StrMatch */
  88.  
  89.         do {
  90.            if (ptr == &buf[BUFSIZ-1])
  91.              ptr = buf;          /* Save last character for term \0  */
  92.            if (sread(ptr , 1, Timeout) < 1)
  93.            {                  /* The scan failed?                    */
  94.               char *s;
  95.               while ( ptr > buf )
  96.                  if (*(--ptr) > ' ')
  97.                     break;    /* Locate the last printable char      */
  98.               *(ptr+1) = '\0';   /* Terminate the string             */
  99.  
  100.               for ( s = buf; (*s > '\0') && (*s <= ' '); s++ );
  101.                                  /* Locate the first printable char  */
  102.  
  103.               while ( ptr-- > s )/* Zap control chars                */
  104.                  if (*ptr < ' ')
  105.                     *ptr = '?';
  106.  
  107.               printmsg(1, "got ??? \"%s\"",s );
  108.               return FALSE;
  109.            }
  110.            *ptr &= 0x7f;
  111.         } while (!StrMatch(NULL, *ptr++));
  112.         return TRUE;
  113.  
  114. } /*expectstr*/
  115.  
  116.  
  117. /*
  118.  *      StrMatch: Incrementally search for a string.
  119.  *      John H. DuBois III  3/31/90
  120.  *      StrMatch searches for a string in a sequence of characters.
  121.  *      The string to search for is passed in an initial setup call.
  122.  *      Further calls with the search string set to NULL pass one
  123.  *      character per call.
  124.  *      The characters are built up into an input string.
  125.  *      After each character is added to the input string,
  126.  *      the search string is compared to the last length(search string)
  127.  *      characters of the input string to determine whether the search
  128.  *      string has been found.
  129.  *
  130.  *      Global variables: none.
  131.  *
  132.  *      Input parameters:
  133.  *      MatchStr is the string to search for.
  134.  *      It is not copied; a static pointer to it is saved.
  135.  *      C is the character to add to the input string.
  136.  *      It is ignored on a setup call.
  137.  *
  138.  *      Output parameters: None.
  139.  *
  140.  *      Return value:
  141.  *      On the setup call, -1 is returned if the search string is
  142.  *      longer than the input string buffer.  Otherwise, 0 is returned.
  143.  *
  144.  *      On comparison calls, 1 is returned if the search string has
  145.  *      been found.  Otherwise 0 is returned.
  146.  */
  147.  
  148.  
  149. static int StrMatch(char *MatchStr, char C)
  150. {
  151. /*
  152.  *      The input string is stored in a circular buffer of MAXMATCH
  153.  *      characters.  If the search string is found in the input,
  154.  *      then the last character added to the buffer will be the last
  155.  *      character of the search string.  Therefore, the string
  156.  *      compare will always start SearchLen characters behind the
  157.  *      position where characters are added to the buffer.
  158.  */
  159.  
  160.         static char Buffer[MAXMATCH];   /* Input string buffer */
  161.         static char *Search;    /* Search string */
  162.         static int  PutPos;     /* Where to add chars to string buffer */
  163.         static int SearchPos;   /* Where in buffer to start string compare */
  164.         int SearchLen;          /* Length of search string */
  165.         int BufInd;             /* Index to input string buffer for string
  166.                                    compare */
  167.         char *SearchInd;        /* Index to search string for string
  168.                                    compare */
  169.  
  170.         if (MatchStr) {                 /* Set up call */
  171.                 Search = MatchStr;
  172.                 SearchLen = strlen(Search);
  173.                 if (SearchLen > MAXMATCH) {
  174.                    printmsg(0,"StrMatch: String to match '%s' is too long.\n",
  175.                         Search);
  176.                    return(-1);
  177.                 }
  178.                 memset(Buffer,'\0',MAXMATCH);   /* Clear buffer */
  179.                 PutPos = 0;
  180.                 SearchPos = MAXMATCH - SearchLen;
  181.                 return 0;
  182.         }
  183.         Buffer[ PutPos++ & QINDMASK] = C;
  184.         for (BufInd = ++SearchPos,SearchInd = Search; *SearchInd; SearchInd++)
  185.                 if (Buffer[BufInd++ & QINDMASK] != *SearchInd)
  186.                         return 0;
  187.  
  188.         return 1;
  189. }
  190.  
  191. /*--------------------------------------------------------------------*/
  192. /*    w r i t e s t r                                                 */
  193. /*                                                                    */
  194. /*    Send a string to the port during login                          */
  195. /*--------------------------------------------------------------------*/
  196.  
  197. static boolean writestr(register char *s)
  198. {
  199.    register char last = '\0';
  200.    boolean nocr  = FALSE;
  201.    unsigned char digit;
  202.  
  203.    if equal(s,"BREAK")
  204.    {
  205.       ssendbrk(0);
  206.       return TRUE;               /* Don't bother with a CR after this   */
  207.    }
  208.    while (*s) {
  209.       if (last == '\\') {
  210.          last = *s;
  211.          switch (*s) {
  212.          case 'd':   /* delay */
  213.          case 'D':
  214.             ssleep(2);
  215.             break;
  216.          case 'c':   /* don't output CR at end of string */
  217.          case 'C':
  218.             nocr = TRUE;
  219.             break;
  220.          case 'r':   /* carriage return */
  221.          case 'R':
  222.          case 'm':
  223.          case 'M':
  224.             slowwrite("\r", 1);
  225.             break;
  226.          case 'n':   /* new line */
  227.          case 'N':
  228.             slowwrite("\n", 1);
  229.             break;
  230.          case 'p':   /* delay */
  231.          case 'P':
  232.             ddelay(400);
  233.             break;
  234.          case 'b':   /* backspace */
  235.          case 'B':
  236.             slowwrite("\b", 1);
  237.             break;
  238.          case 't':   /* tab */
  239.          case 'T':
  240.             slowwrite("\t", 1);
  241.             break;
  242.          case 's':   /* space */
  243.          case 'S':
  244.             slowwrite(" ", 1);
  245.             break;
  246.          case 'z':   /* set serial port speed */
  247.          case 'Z':
  248.             SIOSpeed(atoi(++s));
  249.             while (*s != '\0' && *s != '\\')
  250.                s++;
  251.             if (*s == '\\')
  252.                s++;
  253.             break;
  254.  
  255.          case '0':
  256.          case '1':
  257.          case '2':
  258.          case '3':
  259.          case '4':
  260.          case '5':
  261.          case '6':
  262.          case '7':
  263.             digit = 0;
  264.             while( (*s >= '0') && (*s < '8'))
  265.                digit = (unsigned char) (digit * 8 + *s++ - '0');
  266.             s--;              /* Backup before non-numeric char      */
  267.             slowwrite((char *) &digit,1);
  268.             break;
  269.  
  270.          default: /* ordinary character */
  271.             slowwrite(s, 1);
  272.             last = '\0';      /* Zap any repeated backslash (\)      */
  273.          }
  274.       }
  275.       else if (*s != '\\') /* backslash */
  276.          slowwrite(s, 1);
  277.       else
  278.          last = *s;
  279.       s++;
  280.    }
  281.  
  282.    return nocr;
  283.  
  284. } /*writestr*/
  285.  
  286.  
  287. /*
  288.    s e n d s t r
  289.  
  290.    Send line of login sequence
  291. */
  292.  
  293. void sendstr(char *str)
  294. {
  295.    printmsg(2, "sending \"%s\"", str);
  296.  
  297.    if (equaln(str, "BREAK", 5)) {
  298.       int   nulls;
  299.       nulls = atoi(&str[5]);
  300.       if (nulls <= 0 || nulls > 10)
  301.          nulls = 3;
  302.       ssendbrk(nulls);  /* send a break signal */
  303.       return;
  304.    }
  305.  
  306.    if (equal(str, "EOT")) {
  307.       slowwrite(EOTMSG, strlen(EOTMSG));
  308.       return;
  309.    }
  310.  
  311.    if (equal(str, "\"\""))
  312.       *str = '\0';
  313.  
  314.    if (!equal(str,"")) {
  315.       if (!writestr(str)) {
  316.          slowwrite("\r", 1);
  317.       }
  318.    } else
  319.       slowwrite("\r", 1);
  320.    return;
  321.  
  322. } /*sendstr*/
  323.